package com.linkage.service.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.linkage.pojo.commodity.Book;
import com.linkage.repository.background.BookManageRepository;
import com.linkage.repository.commodity.CategoryRepository;

/**
 * @标题：图书后台管理业务
 * @描述：图书业务操作
 * @author: 邓达
 */

@Service
public class BookManageService {
	
	@Autowired
	private BookManageRepository bookManageRepository;
  
	@Autowired CategoryRepository categoryRepository;
	//FIXME
	public boolean addNewBook(Book newbook){

	    if("".equals(newbook.getBookAuthor()) 
	            || newbook.getBookAuthor() == null
	            || StringUtils.isEmpty(newbook.getBookAuthor())
	            || "".equals(newbook.getBookName())
	            || newbook.getBookName() == null
	            ||  StringUtils.isEmpty(newbook.getBookName())
	            || newbook.getISBN()== null
	            ||  StringUtils.isEmpty(newbook.getISBN())
	            || "".equals(newbook.getISBN())
	            || newbook.getStockNumber() == null
	            || newbook.getStockNumber() < 0
	            || newbook.getCategoryId() == null
	            || !(this.categoryRepository.getCategoryById(newbook.getCategoryId()))
	            ){
	        return false;
	        
	    }

	    newbook.setAverageNumber(0);
	    newbook.setRaterNumber(0);
	    newbook.setIsAvailable(true);
	    this.bookManageRepository.saveBook(newbook);
	   return true;
	}
	

	//更改书本状态
	public void changeBookStatus(@Param("bookId") Integer bookId,@Param("newStatus") Boolean newStatus){
	    
	    if(this.bookManageRepository.getBookByID(bookId) != null){
	        this.bookManageRepository.changeBookStatus(bookId, newStatus);
	    }
	
	}

	//保存图书
	public void saveBook(Book book){
		this.bookManageRepository.saveBook(book);
		
	}
	
	//列出所有图书
	public List<Map<String, Object>> listAllBooks(){
		return this.bookManageRepository.listAllBooks();
	}
	
	
	//编辑图书
	public boolean editBook(Book newbook){
	    
	    
        if ("".equals(newbook.getBookAuthor()) 
                || newbook.getBookAuthor() == null
                || "".equals(newbook.getBookName())
                || newbook.getBookName() == null
                || newbook.getISBN()== null
                || "".equals(newbook.getISBN())
                || newbook.getStockNumber() == null
                || newbook.getStockNumber() < 0
                ||!(this.categoryRepository.getCategoryById(newbook.getCategoryId()))
                || StringUtils.isEmpty(newbook.getBookAuthor()) 
                || StringUtils.isEmpty(newbook.getBookName())
                || StringUtils.isEmpty(newbook.getISBN())
                ){

            return false;
        }
        this.bookManageRepository.editBook(newbook);
		return true;
		
	}
	
	
	   public  List<Map<String, Object>> listSpecialList(){
	       return this.bookManageRepository.listSpecialList();
	   }
	
	   
	    public Book getBookByID(Integer bookId){
	        return this.bookManageRepository.getBookByID(bookId);
	    }
	   
	
	


}
